/**
 * Copyright (c) 2018 苍穹跃龙
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.l.web.master.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.l.core.UniqueIDHandler;
import com.l.core.util.SecurityUtils;
import com.l.web.master.system.entity.SysUser;
import com.l.web.master.system.entity.SysUserRole;
import com.l.web.master.system.mapper.SysUserMapper;
import com.l.web.master.system.service.ISysUserRoleService;
import com.l.web.master.system.service.ISysUserService;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 严秋旺
 * @since 2018-09-21
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private UniqueIDHandler uniqueIDHandler;

    @Autowired
    private ISysUserRoleService userRoleService;

    private void insertUserRole(SysUser sysUser, String[] roleIds) {
        if (roleIds == null || roleIds.length == 0) {
            return;
        }

        List<SysUserRole> list = new ArrayList<>();

        for (String roleId : roleIds) {
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(sysUser.getId()).setRoleId(roleId);
            list.add(userRole);
        }

        userRoleService.insertBatch(list);
    }

    @Transactional
    @Override
    public void insert(SysUser sysUser, String[] roleIds) {
        sysUser.setId(uniqueIDHandler.userId());
        String encryptPassword = encryptPassword(sysUser);
        sysUser.setPassword(encryptPassword);
        baseMapper.insert(sysUser);
        insertUserRole(sysUser, roleIds);
    }

    @Transactional
    @Override
    public void updateById(SysUser sysUser, String[] roleIds) {
        String encryptPassword = encryptPassword(sysUser);
        sysUser.setPassword(encryptPassword);
        baseMapper.updateById(sysUser);

        EntityWrapper<SysUserRole> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", sysUser.getId());
        userRoleService.delete(wrapper);

        insertUserRole(sysUser, roleIds);
    }

    @Override
    public SysUser findUserByUserName(String userName) {
        EntityWrapper<SysUser> wrapper = new EntityWrapper<>();
        wrapper.eq("user_name", userName);
        List<SysUser> list = baseMapper.selectList(wrapper);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public String encryptPassword(SysUser sysUser) {
        String password = sysUser.getPassword();

        if (StringUtils.isEmpty(password)) {
            return null;
        }

        StringBuilder sb = new StringBuilder(password);
        sb.insert(password.length() / 2, sysUser.getId());
        String md5 = SecurityUtils.md5(sb.toString());
        return md5;
    }

}
